<!-- build time:Thu May 04 2023 15:06:03 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#FFF"><link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon.png"><link rel="icon" type="image/ico" sizes="32x32" href="/blog/images/favicon.ico"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link rel="alternate" type="application/rss+xml" title="物競天擇" href="https://furanzhang.gitee.io/blog/rss.xml"><link rel="alternate" type="application/atom+xml" title="物競天擇" href="https://furanzhang.gitee.io/blog/atom.xml"><link rel="alternate" type="application/json" title="物競天擇" href="https://furanzhang.gitee.io/blog/feed.json"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Mulish:300,300italic,400,400italic,700,700italic%7CFredericka%20the%20Great:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20JP:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20SC:300,300italic,400,400italic,700,700italic%7CInconsolata:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext"><link rel="stylesheet" href="/blog/css/app.css?v=0.2.5"><meta name="keywords" content="Unity,Python"><link rel="canonical" href="https://furanzhang.gitee.io/blog/2021/08/03/%E8%B4%B4%E5%9B%BE2%E6%AC%A1%E5%B9%82%E6%89%B9%E9%87%8F%E4%BF%AE%E6%AD%A3%E5%B7%A5%E5%85%B7%EF%BC%8C%E5%9F%BA%E4%BA%8EPython/"><title>贴图2次幂批量修正工具，基于Python | Frz = 物競天擇 = 最怕你一生碌碌无为，还安慰自己平凡可贵</title><meta name="generator" content="Hexo 5.3.0"></head><body itemscope itemtype="http://schema.org/WebPage"><div id="loading"><div class="cat"><div class="body"></div><div class="head"><div class="face"></div></div><div class="foot"><div class="tummy-end"></div><div class="bottom"></div><div class="legs left"></div><div class="legs right"></div></div><div class="paw"><div class="hands left"></div><div class="hands right"></div></div></div></div><div id="container"><header id="header" itemscope itemtype="http://schema.org/WPHeader"><div class="inner"><div id="brand"><div class="pjax"><h1 itemprop="name headline">贴图2次幂批量修正工具，基于Python</h1><div class="meta"><span class="item" title="创建时间：2021-08-03 16:55:26"><span class="icon"><i class="ic i-calendar"></i> </span><span class="text">发表于</span> <time itemprop="dateCreated datePublished" datetime="2021-08-03T16:55:26+08:00">2021-08-03</time> </span><span class="item" title="本文字数"><span class="icon"><i class="ic i-pen"></i> </span><span class="text">本文字数</span> <span>6.2k</span> <span class="text">字</span> </span><span class="item" title="阅读时长"><span class="icon"><i class="ic i-clock"></i> </span><span class="text">阅读时长</span> <span>6 分钟</span></span></div></div></div><nav id="nav"><div class="inner"><div class="toggle"><div class="lines" aria-label="切换导航栏"><span class="line"></span> <span class="line"></span> <span class="line"></span></div></div><ul class="menu"><li class="item title"><a href="/blog/" rel="start">Frz</a></li></ul><ul class="right"><li class="item theme"><i class="ic i-sun"></i></li><li class="item search"><i class="ic i-search"></i></li></ul></div></nav></div><div id="imgs" class="pjax"><ul><li class="item" data-background-image="/blog/Shoka_images/6833939bly1giclwrdwyaj20zk0m8are.jpg"></li><li class="item" data-background-image="/blog/Shoka_images/6833939bly1giclxp31goj20zk0m8qv5.jpg"></li><li class="item" data-background-image="/blog/Shoka_images/6833939bly1giclj61ylzj20zk0m8b29.jpg"></li><li class="item" data-background-image="/blog/Shoka_images/6833939bly1gipevgoki5j20zk0m84qp.jpg"></li><li class="item" data-background-image="/blog/Shoka_images/6833939bly1giclh3brzpj20zk0m8ann.jpg"></li><li class="item" data-background-image="/blog/Shoka_images/6833939bly1giclga70tsj20zk0m84mr.jpg"></li></ul></div></header><div id="waves"><svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z"/></defs><g class="parallax"><use xlink:href="#gentle-wave" x="48" y="0"/><use xlink:href="#gentle-wave" x="48" y="3"/><use xlink:href="#gentle-wave" x="48" y="5"/><use xlink:href="#gentle-wave" x="48" y="7"/></g></svg></div><main><div class="inner"><div id="main" class="pjax"><div class="article wrap"><div class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList"><i class="ic i-home"></i> <span><a href="/blog/">首页</a></span></div><article itemscope itemtype="http://schema.org/Article" class="post block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://furanzhang.gitee.io/blog/2021/08/03/%E8%B4%B4%E5%9B%BE2%E6%AC%A1%E5%B9%82%E6%89%B9%E9%87%8F%E4%BF%AE%E6%AD%A3%E5%B7%A5%E5%85%B7%EF%BC%8C%E5%9F%BA%E4%BA%8EPython/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/blog/images/tongZhi.webp"><meta itemprop="name" content="Frz"><meta itemprop="description" content="最怕你一生碌碌无为，还安慰自己平凡可贵, 自卑不是谦逊 | 傲慢不是自信"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="物競天擇"></span><div class="body md" itemprop="articleBody"><div class="note danger no-icon"><p>年初时用 Python 写的一个图片分辨率 2 次幂批量修正的工具。</p><p>贴图、图片压缩分辨率必须是 2 次幂才行。</p><p>通过手动修改、一张张去找用格式工厂修改，显然不切实际。</p><p>于是下班回家花了两小时边学 python 边写了这个工具。</p></div><h5 id="一-工具下载"><a class="markdownIt-Anchor" href="#一-工具下载">#</a> 一、工具下载</h5><p>我把工具放在了阿里云盘：</p><blockquote><p>下载链接：<span class="exturl" data-url="aHR0cHM6Ly93d3cuYWxpeXVuZHJpdmUuY29tL3MvV2J0anhyRmJhM1k=">https://www.aliyundrive.com/s/WbtjxrFba3Y</span></p><p>因为阿里云目前不支持压缩包格式分享</p><p>文件格式为 jpg，下载后改后缀为 rar 并解压即可</p></blockquote><h5 id="二-用法"><a class="markdownIt-Anchor" href="#二-用法">#</a> 二、用法</h5><blockquote><p>1. 解压后编辑 textureAmentDic.txt，内容修改为你要批量处理图片的文件夹</p><p>2. 运行 textureAment.exe，等待批量修正完成即可。</p></blockquote><p>另外，在当前的版本中，我加入了文件夹选择功能。</p><blockquote><p>删掉 textureAmentDic2.txt 文件，运行 textureAment.exe，就可以通过资源管理器选择需要批量处理的图片的文件夹</p></blockquote><h5 id="三-工具源码"><a class="markdownIt-Anchor" href="#三-工具源码">#</a> 三、工具源码</h5><p>也可以复制源码自行编译</p><figure class="highlight python"><figcaption data-lang="python"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment"># from PyQt5.QtGui import *</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">from</span> cv2 <span class="token keyword">import</span> cv2 </pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> os</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> warnings</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> tkinter <span class="token keyword">as</span> tk</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">from</span> tkinter <span class="token keyword">import</span> filedialog</pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token comment"># file_dir = 'E:/zuomian/ 贴图'</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token comment"># for root, dirs, files in os.walk(file_dir):</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token comment">#     print ("========================================")</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token comment">#     print ("root : &#123;0&#125;".format(root))</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token comment">#     print ("dirs : &#123;0&#125;".format(dirs))</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token comment">#     print ("files : &#123;0&#125;".format(files))</span></pre></td></tr><tr><td data-num="16"></td><td><pre></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token comment"># for file in files:</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token comment">#     try:</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token comment">#         print('-----------------------------------')</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token comment">#         file_name = os.path.splitext(file)[0]</span></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token comment">#         file_suffix = os.path.splitext(file)[1]</span></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token comment">#         file_path = os.path.join(root, file)</span></pre></td></tr><tr><td data-num="23"></td><td><pre><span class="token comment">#         file_abs_path = os.path.abspath(file)</span></pre></td></tr><tr><td data-num="24"></td><td><pre><span class="token comment">#         file_parent = os.path.dirname(file_path)</span></pre></td></tr><tr><td data-num="25"></td><td><pre></pre></td></tr><tr><td data-num="26"></td><td><pre><span class="token comment">#         print ("file : &#123;0&#125;".format(file))</span></pre></td></tr><tr><td data-num="27"></td><td><pre><span class="token comment">#         print ("file_name : &#123;0&#125;".format(file_name))</span></pre></td></tr><tr><td data-num="28"></td><td><pre><span class="token comment">#         print ("file_suffix : &#123;0&#125;".format(file_suffix))</span></pre></td></tr><tr><td data-num="29"></td><td><pre><span class="token comment">#         print ("file_path : &#123;0&#125;".format(file_path))</span></pre></td></tr><tr><td data-num="30"></td><td><pre><span class="token comment">#         print ("file_abs_path : &#123;0&#125;".format(file_abs_path))</span></pre></td></tr><tr><td data-num="31"></td><td><pre><span class="token comment">#         print ("file_parent : &#123;0&#125;".format(file_parent))</span></pre></td></tr><tr><td data-num="32"></td><td><pre><span class="token comment">#     except:</span></pre></td></tr><tr><td data-num="33"></td><td><pre><span class="token comment">#         print ("Exception")</span></pre></td></tr><tr><td data-num="34"></td><td><pre></pre></td></tr><tr><td data-num="35"></td><td><pre>totalNum <span class="token operator">=</span> <span class="token number">0</span></pre></td></tr><tr><td data-num="36"></td><td><pre><span class="token comment"># img = QImage()</span></pre></td></tr><tr><td data-num="37"></td><td><pre>file_dir <span class="token operator">=</span> <span class="token string">''</span></pre></td></tr><tr><td data-num="38"></td><td><pre></pre></td></tr><tr><td data-num="39"></td><td><pre>warnings<span class="token punctuation">.</span>filterwarnings<span class="token punctuation">(</span><span class="token string">"ignore"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="40"></td><td><pre></pre></td></tr><tr><td data-num="41"></td><td><pre><span class="token comment"># 读取文件中的路径</span></pre></td></tr><tr><td data-num="42"></td><td><pre>file_path <span class="token operator">=</span> <span class="token string">'textureAmentDic.txt'</span></pre></td></tr><tr><td data-num="43"></td><td><pre><span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isfile<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="44"></td><td><pre>    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> file_path <span class="token operator">+</span> <span class="token string">'文件存在'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="45"></td><td><pre>    f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>file_path<span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="46"></td><td><pre>    file_dir <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="47"></td><td><pre>    <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isdir<span class="token punctuation">(</span>file_dir<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="48"></td><td><pre>        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> <span class="token string">'路径存在： '</span> <span class="token operator">+</span> file_dir<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="49"></td><td><pre>    <span class="token keyword">else</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="50"></td><td><pre>        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> <span class="token string">'路径不存在！！ '</span> <span class="token operator">+</span> file_dir<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="51"></td><td><pre><span class="token keyword">else</span><span class="token punctuation">:</span>    </pre></td></tr><tr><td data-num="52"></td><td><pre>    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> file_path <span class="token operator">+</span> <span class="token string">'文件不存在！！'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="53"></td><td><pre></pre></td></tr><tr><td data-num="54"></td><td><pre><span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>file_dir<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword">or</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isdir<span class="token punctuation">(</span>file_dir<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="55"></td><td><pre>    <span class="token comment"># 资源管理器路径</span></pre></td></tr><tr><td data-num="56"></td><td><pre>    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> <span class="token string">'启用资源管理器选择路径 '</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="57"></td><td><pre>    root <span class="token operator">=</span> tk<span class="token punctuation">.</span>Tk<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="58"></td><td><pre>    root<span class="token punctuation">.</span>withdraw<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="59"></td><td><pre>    file_dir <span class="token operator">=</span> filedialog<span class="token punctuation">.</span>askdirectory<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="60"></td><td><pre>    <span class="token keyword">if</span> <span class="token keyword">not</span> file_dir <span class="token keyword">or</span> <span class="token builtin">len</span><span class="token punctuation">(</span>file_dir<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>   </pre></td></tr><tr><td data-num="61"></td><td><pre>        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> <span class="token string">"未选择有效路径！！"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="62"></td><td><pre>    <span class="token keyword">else</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="63"></td><td><pre>        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'>>'</span> <span class="token operator">+</span> <span class="token string">'选中路径 '</span> <span class="token operator">+</span> file_dir<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="64"></td><td><pre></pre></td></tr><tr><td data-num="65"></td><td><pre><span class="token comment"># 将 image = cv2.imread (image_path) 替换为：image = cv2.imdecode (np.fromfile (image_path,dtype=np.uint8),-1) 即可。</span></pre></td></tr><tr><td data-num="66"></td><td><pre><span class="token comment"># 同样，如果要保存图像为中文文件名，则将 cv2.imwrite (output_image_path, image) 替换为 cv2.imencode ('.jpg', image)[1].tofile (output_image_path) 即可。</span></pre></td></tr><tr><td data-num="67"></td><td><pre></pre></td></tr><tr><td data-num="68"></td><td><pre><span class="token keyword">for</span> root<span class="token punctuation">,</span> dirs<span class="token punctuation">,</span> files <span class="token keyword">in</span> os<span class="token punctuation">.</span>walk<span class="token punctuation">(</span>file_dir<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="69"></td><td><pre>    <span class="token keyword">for</span> <span class="token builtin">file</span> <span class="token keyword">in</span> files<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="70"></td><td><pre>        <span class="token keyword">try</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="71"></td><td><pre>            file_suffix <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>splitext<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="72"></td><td><pre>            image_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="73"></td><td><pre>            <span class="token keyword">if</span> file_suffix <span class="token operator">==</span> <span class="token string">'.jpg'</span> <span class="token keyword">or</span> file_suffix <span class="token operator">==</span> <span class="token string">'.png'</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="74"></td><td><pre>                <span class="token comment"># img.load(image_path)</span></pre></td></tr><tr><td data-num="75"></td><td><pre>                <span class="token comment"># img.save(image_path)</span></pre></td></tr><tr><td data-num="76"></td><td><pre>                image <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imdecode<span class="token punctuation">(</span>np<span class="token punctuation">.</span>fromfile<span class="token punctuation">(</span>image_path<span class="token punctuation">,</span> dtype<span class="token operator">=</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="77"></td><td><pre>                <span class="token keyword">if</span> image<span class="token punctuation">.</span>size <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span> <span class="token comment"># not isinstance(image,type(None)) and</span></pre></td></tr><tr><td data-num="78"></td><td><pre>                    a <span class="token operator">=</span> image<span class="token punctuation">.</span>shape</pre></td></tr><tr><td data-num="79"></td><td><pre>                    <span class="token comment"># print(a)</span></pre></td></tr><tr><td data-num="80"></td><td><pre>                    w <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="81"></td><td><pre>                    h <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="82"></td><td><pre>                    <span class="token keyword">if</span> w <span class="token operator">></span> <span class="token number">4</span> <span class="token keyword">and</span> h <span class="token operator">></span> <span class="token number">4</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="83"></td><td><pre>                        wMod <span class="token operator">=</span> w <span class="token operator">%</span> <span class="token number">4</span></pre></td></tr><tr><td data-num="84"></td><td><pre>                        hMod <span class="token operator">=</span> h <span class="token operator">%</span> <span class="token number">4</span></pre></td></tr><tr><td data-num="85"></td><td><pre>                        <span class="token keyword">if</span> wMod <span class="token operator">!=</span> <span class="token number">0</span> <span class="token keyword">or</span> hMod <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="86"></td><td><pre>                            <span class="token keyword">if</span> wMod <span class="token operator">></span> <span class="token number">2</span> <span class="token keyword">or</span> hMod <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="87"></td><td><pre>                                <span class="token keyword">if</span> wMod <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="88"></td><td><pre>                                    w <span class="token operator">=</span> w <span class="token operator">+</span> <span class="token number">4</span> <span class="token operator">-</span> wMod</pre></td></tr><tr><td data-num="89"></td><td><pre>                                <span class="token keyword">if</span> hMod <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="90"></td><td><pre>                                    h <span class="token operator">=</span> h <span class="token operator">+</span> <span class="token number">4</span> <span class="token operator">-</span> hMod</pre></td></tr><tr><td data-num="91"></td><td><pre>                            <span class="token keyword">else</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="92"></td><td><pre>                                <span class="token keyword">if</span> wMod <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="93"></td><td><pre>                                    w <span class="token operator">=</span> w <span class="token operator">-</span> wMod</pre></td></tr><tr><td data-num="94"></td><td><pre>                                <span class="token keyword">if</span> hMod <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="95"></td><td><pre>                                    h <span class="token operator">=</span> h <span class="token operator">-</span> hMod</pre></td></tr><tr><td data-num="96"></td><td><pre></pre></td></tr><tr><td data-num="97"></td><td><pre>                            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"| &#123;0&#125; | 原宽高：&#123;1&#125;,&#123;2&#125; | 修正后宽高：&#123;3&#125;,&#123;4&#125;"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> w <span class="token punctuation">,</span> h<span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="98"></td><td><pre>                            totalNum <span class="token operator">+=</span> <span class="token number">1</span></pre></td></tr><tr><td data-num="99"></td><td><pre>                            <span class="token comment"># 三次插值法</span></pre></td></tr><tr><td data-num="100"></td><td><pre>                            p0 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>image<span class="token punctuation">,</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span>h<span class="token punctuation">)</span><span class="token punctuation">,</span>interpolation<span class="token operator">=</span>cv2<span class="token punctuation">.</span>INTER_CUBIC<span class="token punctuation">)</span>   </pre></td></tr><tr><td data-num="101"></td><td><pre>                            cv2<span class="token punctuation">.</span>imencode<span class="token punctuation">(</span>file_suffix<span class="token punctuation">,</span> p0<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>tofile<span class="token punctuation">(</span>image_path<span class="token punctuation">)</span>       </pre></td></tr><tr><td data-num="102"></td><td><pre>                            <span class="token comment"># print("WWWWW111 &#123;0&#125;".format(file))                      </span></pre></td></tr><tr><td data-num="103"></td><td><pre>                <span class="token comment"># cv2.imwrite(image_path,p0)    </span></pre></td></tr><tr><td data-num="104"></td><td><pre>                <span class="token comment"># print("WWWWW222 &#123;0&#125;".format(file)) </span></pre></td></tr><tr><td data-num="105"></td><td><pre>                <span class="token comment"># p0_exists ='p0' in locals() or 'p0' in globals() </span></pre></td></tr><tr><td data-num="106"></td><td><pre>                <span class="token comment"># if p0_exists and not isinstance(p0,type(None)):  # p0 != None</span></pre></td></tr><tr><td data-num="107"></td><td><pre>                <span class="token comment">#     cv2.imencode(file_suffix, p0)[1].tofile(image_path)    </span></pre></td></tr><tr><td data-num="108"></td><td><pre>                <span class="token comment">#     # print("WWWWW333 &#123;0&#125;".format(file)) </span></pre></td></tr><tr><td data-num="109"></td><td><pre>                <span class="token comment">#     p0 = None    </span></pre></td></tr><tr><td data-num="110"></td><td><pre>                <span class="token comment">#     # print("WWWWW444 &#123;0&#125;".format(file)) </span></pre></td></tr><tr><td data-num="111"></td><td><pre>                <span class="token comment"># else:</span></pre></td></tr><tr><td data-num="112"></td><td><pre>                <span class="token comment">#     cv2.imencode(file_suffix, image)[1].tofile(image_path)</span></pre></td></tr><tr><td data-num="113"></td><td><pre></pre></td></tr><tr><td data-num="114"></td><td><pre>        <span class="token keyword">except</span> Exception <span class="token keyword">as</span> err<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="115"></td><td><pre>            <span class="token comment"># print("&#123;0&#125; | &#123;1&#125;".format(file, err))</span></pre></td></tr><tr><td data-num="116"></td><td><pre>            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"跳过文件 | &#123;0&#125;"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="117"></td><td><pre>        <span class="token comment"># finally:       </span></pre></td></tr><tr><td data-num="118"></td><td><pre>        <span class="token comment">#     print ("跳过文件 | &#123;0&#125;".format (file))</span></pre></td></tr><tr><td data-num="119"></td><td><pre></pre></td></tr><tr><td data-num="120"></td><td><pre><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"==》》 处理图片数量：&#123;0&#125; 《《=="</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>totalNum<span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="121"></td><td><pre></pre></td></tr><tr><td data-num="122"></td><td><pre>os<span class="token punctuation">.</span>system<span class="token punctuation">(</span><span class="token string">'pause'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="123"></td><td><pre></pre></td></tr><tr><td data-num="124"></td><td><pre><span class="token comment"># image = cv2.imread(file)</span></pre></td></tr><tr><td data-num="125"></td><td><pre></pre></td></tr><tr><td data-num="126"></td><td><pre></pre></td></tr><tr><td data-num="127"></td><td><pre><span class="token comment"># 重采样插值法（cv2.INTER_AREA）</span></pre></td></tr><tr><td data-num="128"></td><td><pre></pre></td></tr><tr><td data-num="129"></td><td><pre><span class="token comment"># 等比缩放</span></pre></td></tr><tr><td data-num="130"></td><td><pre><span class="token comment"># p1=cv2.resize(image,(int(a[1]/1.5),int(a[0]/1.5)),</span></pre></td></tr><tr><td data-num="131"></td><td><pre><span class="token comment">#                interpolation=cv2.INTER_CUBIC)</span></pre></td></tr></table></figure><h5 id="四-其他"><a class="markdownIt-Anchor" href="#四-其他">#</a> 四、其他</h5><p>这里再附上，同事写的一个 tga 转 png 工具，同样基于 python，不过这个没有编译，需要安装 python 环境才能使用。</p><p>unity 中 tga 批量转为 png 可以有效减小资源大小。</p><figure class="highlight python"><figcaption data-lang="python"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">#!/usr/bin/env python</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token comment"># coding: utf-8</span></pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> os</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> time</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> hashlib</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> subprocess</pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process<span class="token punctuation">,</span> Queue</pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre>queue <span class="token operator">=</span> Queue<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre>workingFolder <span class="token operator">=</span> <span class="token string">r"E:\UnityProject\MyProject\Assets"</span></pre></td></tr><tr><td data-num="13"></td><td><pre>logFile <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">r"E:\zuomian\zuomian\converttgatopng\log.log"</span><span class="token punctuation">,</span> <span class="token string">"w"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre></pre></td></tr><tr><td data-num="16"></td><td><pre>enable_run_command <span class="token operator">=</span> <span class="token boolean">False</span></pre></td></tr><tr><td data-num="17"></td><td><pre>enable_run_command <span class="token operator">=</span> <span class="token boolean">True</span></pre></td></tr><tr><td data-num="18"></td><td><pre></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token keyword">def</span> <span class="token function">run_command</span><span class="token punctuation">(</span>cmd<span class="token punctuation">,</span> callback<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token keyword">if</span> <span class="token keyword">not</span> enable_run_command<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="21"></td><td><pre>        <span class="token keyword">return</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token comment"># TIME = time.time()</span></pre></td></tr><tr><td data-num="23"></td><td><pre>    <span class="token comment"># print("[START COMMMAND]start at:", ftime(TIME))</span></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token comment"># print("[CMD]", cmd)</span></pre></td></tr><tr><td data-num="25"></td><td><pre>    result <span class="token operator">=</span> subprocess<span class="token punctuation">.</span>call<span class="token punctuation">(</span>cmd<span class="token punctuation">,</span> shell<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="26"></td><td><pre>    <span class="token comment"># ENDTIME = time.time()</span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token comment"># print("[END COMMMAND]start at: %s  ending at: %s  dur: %s\n\n" %(ftime(TIME), ftime(ENDTIME),</span></pre></td></tr><tr><td data-num="28"></td><td><pre>    <span class="token comment">#     format_relate_time(ENDTIME-TIME)))</span></pre></td></tr><tr><td data-num="29"></td><td><pre>    <span class="token keyword">return</span> result</pre></td></tr><tr><td data-num="30"></td><td><pre></pre></td></tr><tr><td data-num="31"></td><td><pre><span class="token keyword">def</span> <span class="token function">md5_checksum</span><span class="token punctuation">(</span>file_path<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="32"></td><td><pre>    <span class="token triple-quoted-string string">'''计算一个文件的MD5值'''</span></pre></td></tr><tr><td data-num="33"></td><td><pre>    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>file_path<span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> fh<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="34"></td><td><pre>        m <span class="token operator">=</span> hashlib<span class="token punctuation">.</span>md5<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="35"></td><td><pre>        <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="36"></td><td><pre>            data <span class="token operator">=</span> fh<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token number">8192</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="37"></td><td><pre>            <span class="token keyword">if</span> <span class="token keyword">not</span> data<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="38"></td><td><pre>                <span class="token keyword">break</span></pre></td></tr><tr><td data-num="39"></td><td><pre>            m<span class="token punctuation">.</span>update<span class="token punctuation">(</span>data<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="40"></td><td><pre>        <span class="token keyword">return</span> m<span class="token punctuation">.</span>hexdigest<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="41"></td><td><pre></pre></td></tr><tr><td data-num="42"></td><td><pre><span class="token keyword">def</span> <span class="token function">produce_file</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="43"></td><td><pre>    <span class="token triple-quoted-string string">'''返回一个目录下所有的文件'''</span></pre></td></tr><tr><td data-num="44"></td><td><pre>    all_files <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="45"></td><td><pre>    <span class="token keyword">for</span> parent<span class="token punctuation">,</span>dirnames<span class="token punctuation">,</span>filenames <span class="token keyword">in</span> os<span class="token punctuation">.</span>walk<span class="token punctuation">(</span>workingFolder<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="46"></td><td><pre>        <span class="token keyword">for</span> filename <span class="token keyword">in</span> filenames<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="47"></td><td><pre>            filepath <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>parent<span class="token punctuation">,</span> filename<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="48"></td><td><pre></pre></td></tr><tr><td data-num="49"></td><td><pre>            <span class="token comment"># if u"BDZ_BYQ_S_N.tga.meta" in filepath:</span></pre></td></tr><tr><td data-num="50"></td><td><pre>            <span class="token comment">#     print("FUCK")</span></pre></td></tr><tr><td data-num="51"></td><td><pre></pre></td></tr><tr><td data-num="52"></td><td><pre>            origin_ext <span class="token operator">=</span> <span class="token string">u".tga.meta"</span></pre></td></tr><tr><td data-num="53"></td><td><pre>            <span class="token keyword">if</span> filepath<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>endswith<span class="token punctuation">(</span>origin_ext<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="54"></td><td><pre>                <span class="token comment"># print(filepath)</span></pre></td></tr><tr><td data-num="55"></td><td><pre>                </pre></td></tr><tr><td data-num="56"></td><td><pre>                base_filepath <span class="token operator">=</span> filepath<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token builtin">len</span><span class="token punctuation">(</span>origin_ext<span class="token punctuation">)</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="57"></td><td><pre>                tga_filepath <span class="token operator">=</span> base_filepath <span class="token operator">+</span> <span class="token string">u".tga"</span></pre></td></tr><tr><td data-num="58"></td><td><pre>                png_filepath <span class="token operator">=</span> base_filepath <span class="token operator">+</span> <span class="token string">u".png"</span></pre></td></tr><tr><td data-num="59"></td><td><pre></pre></td></tr><tr><td data-num="60"></td><td><pre>                <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>png_filepath<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="61"></td><td><pre>                    logFile<span class="token punctuation">.</span>write<span class="token punctuation">(</span>png_filepath<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="62"></td><td><pre>                    <span class="token keyword">continue</span></pre></td></tr><tr><td data-num="63"></td><td><pre></pre></td></tr><tr><td data-num="64"></td><td><pre>                all_files<span class="token punctuation">.</span>append<span class="token punctuation">(</span>filepath<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="65"></td><td><pre></pre></td></tr><tr><td data-num="66"></td><td><pre>    <span class="token keyword">return</span> all_files</pre></td></tr><tr><td data-num="67"></td><td><pre></pre></td></tr><tr><td data-num="68"></td><td><pre><span class="token keyword">def</span> <span class="token function">worker</span><span class="token punctuation">(</span>queue<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="69"></td><td><pre>    <span class="token comment"># 从队列中获取文件路径、计算 MD5、插入数据库</span></pre></td></tr><tr><td data-num="70"></td><td><pre>    <span class="token keyword">for</span> filepath <span class="token keyword">in</span> <span class="token builtin">iter</span><span class="token punctuation">(</span>queue<span class="token punctuation">.</span>get<span class="token punctuation">,</span> <span class="token string">"STOP"</span><span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="71"></td><td><pre>        <span class="token comment"># file_md5 = md5_checksum(filepath)</span></pre></td></tr><tr><td data-num="72"></td><td><pre>        <span class="token comment"># insert_mysql(filepath, file_md5)</span></pre></td></tr><tr><td data-num="73"></td><td><pre>        <span class="token comment"># print(filepath, ":", file_md5)</span></pre></td></tr><tr><td data-num="74"></td><td><pre></pre></td></tr><tr><td data-num="75"></td><td><pre>        origin_ext <span class="token operator">=</span> <span class="token string">u".tga.meta"</span></pre></td></tr><tr><td data-num="76"></td><td><pre>        <span class="token keyword">if</span> filepath<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>endswith<span class="token punctuation">(</span>origin_ext<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="77"></td><td><pre>            </pre></td></tr><tr><td data-num="78"></td><td><pre>            base_filepath <span class="token operator">=</span> filepath<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token builtin">len</span><span class="token punctuation">(</span>origin_ext<span class="token punctuation">)</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="79"></td><td><pre>            tga_filepath <span class="token operator">=</span> base_filepath <span class="token operator">+</span> <span class="token string">u".tga"</span></pre></td></tr><tr><td data-num="80"></td><td><pre>            png_filepath <span class="token operator">=</span> base_filepath <span class="token operator">+</span> <span class="token string">u".png"</span></pre></td></tr><tr><td data-num="81"></td><td><pre></pre></td></tr><tr><td data-num="82"></td><td><pre>            <span class="token comment"># if os.path.exists(png_filepath):</span></pre></td></tr><tr><td data-num="83"></td><td><pre>            <span class="token comment">#     logFile.write(png_filepath)</span></pre></td></tr><tr><td data-num="84"></td><td><pre>            <span class="token comment">#     continue</span></pre></td></tr><tr><td data-num="85"></td><td><pre></pre></td></tr><tr><td data-num="86"></td><td><pre>            <span class="token comment"># 001.Convert TGA to PNG</span></pre></td></tr><tr><td data-num="87"></td><td><pre>            <span class="token keyword">try</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="88"></td><td><pre>                <span class="token keyword">if</span> enable_run_command<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="89"></td><td><pre>                    <span class="token keyword">from</span> PIL <span class="token keyword">import</span> Image</pre></td></tr><tr><td data-num="90"></td><td><pre>                    im <span class="token operator">=</span> Image<span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">(</span>tga_filepath<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="91"></td><td><pre>                    im<span class="token punctuation">.</span>save<span class="token punctuation">(</span>png_filepath<span class="token punctuation">,</span> <span class="token string">"PNG"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="92"></td><td><pre>            <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="93"></td><td><pre>                logFile<span class="token punctuation">.</span>write<span class="token punctuation">(</span>filepath<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="94"></td><td><pre>                <span class="token keyword">continue</span></pre></td></tr><tr><td data-num="95"></td><td><pre></pre></td></tr><tr><td data-num="96"></td><td><pre>            <span class="token comment"># 002.Rename</span></pre></td></tr><tr><td data-num="97"></td><td><pre>            <span class="token keyword">try</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="98"></td><td><pre>                <span class="token keyword">if</span> enable_run_command<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="99"></td><td><pre>                    os<span class="token punctuation">.</span>rename<span class="token punctuation">(</span>filepath<span class="token punctuation">,</span> base_filepath <span class="token operator">+</span> <span class="token string">u".png.meta"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="100"></td><td><pre>            <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="101"></td><td><pre>                logFile<span class="token punctuation">.</span>write<span class="token punctuation">(</span>filepath<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="102"></td><td><pre>                <span class="token keyword">continue</span></pre></td></tr><tr><td data-num="103"></td><td><pre></pre></td></tr><tr><td data-num="104"></td><td><pre>            <span class="token comment"># 003.Remove TGA</span></pre></td></tr><tr><td data-num="105"></td><td><pre>            command <span class="token operator">=</span> <span class="token triple-quoted-string string">u'''del "%s"'''</span> <span class="token operator">%</span><span class="token punctuation">(</span>tga_filepath<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="106"></td><td><pre>            <span class="token keyword">print</span><span class="token punctuation">(</span>command<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="107"></td><td><pre>            run_command<span class="token punctuation">(</span>command<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="108"></td><td><pre></pre></td></tr><tr><td data-num="109"></td><td><pre>    <span class="token keyword">return</span> <span class="token boolean">True</span></pre></td></tr><tr><td data-num="110"></td><td><pre></pre></td></tr><tr><td data-num="111"></td><td><pre></pre></td></tr><tr><td data-num="112"></td><td><pre><span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="113"></td><td><pre>    <span class="token comment"># 向队列中填充数据</span></pre></td></tr><tr><td data-num="114"></td><td><pre>    all_files <span class="token operator">=</span> produce_file<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="115"></td><td><pre></pre></td></tr><tr><td data-num="116"></td><td><pre>    <span class="token keyword">for</span> file_path <span class="token keyword">in</span> all_files<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="117"></td><td><pre>        queue<span class="token punctuation">.</span>put<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="118"></td><td><pre></pre></td></tr><tr><td data-num="119"></td><td><pre>    workers <span class="token operator">=</span> <span class="token number">12</span></pre></td></tr><tr><td data-num="120"></td><td><pre>    processes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="121"></td><td><pre></pre></td></tr><tr><td data-num="122"></td><td><pre>    <span class="token keyword">for</span> w <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>workers<span class="token punctuation">)</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="123"></td><td><pre>        process <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>worker<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="124"></td><td><pre>        process<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="125"></td><td><pre>        processes<span class="token punctuation">.</span>append<span class="token punctuation">(</span>process<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="126"></td><td><pre>        queue<span class="token punctuation">.</span>put<span class="token punctuation">(</span><span class="token string">"STOP"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="127"></td><td><pre></pre></td></tr><tr><td data-num="128"></td><td><pre>    <span class="token keyword">for</span> process <span class="token keyword">in</span> processes<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="129"></td><td><pre>        process<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="130"></td><td><pre></pre></td></tr><tr><td data-num="131"></td><td><pre></pre></td></tr><tr><td data-num="132"></td><td><pre><span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="133"></td><td><pre>    main<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr></table></figure><p>修改 workingFolder 和 logFile 值后再运行</p><div class="tags"><a href="/blog/tags/Unity/" rel="tag"><i class="ic i-tag"></i> Unity</a> <a href="/blog/tags/Python/" rel="tag"><i class="ic i-tag"></i> Python</a></div></div><footer><div class="meta"><span class="item"><span class="icon"><i class="ic i-calendar-check"></i> </span><span class="text">更新于</span> <time title="修改时间：2021-08-03 18:12:21" itemprop="dateModified" datetime="2021-08-03T18:12:21+08:00">2021-08-03</time> </span><span id="2021/08/03/贴图2次幂批量修正工具，基于Python/" class="item leancloud_visitors" data-flag-title="贴图2次幂批量修正工具，基于Python" title="阅读次数"><span class="icon"><i class="ic i-eye"></i> </span><span class="text">阅读次数</span> <span class="leancloud-visitors-count"></span> <span class="text">次</span></span></div><div class="reward"><button><i class="ic i-heartbeat"></i> 赞赏</button><p>请我喝[茶]~(￣▽￣)~*</p><div id="qr"><div><img data-src="/blog/images/%E5%BE%AE%E4%BF%A1%E8%B5%9E%E8%B5%8F%E7%A0%81.webp" alt="Frz 微信支付"><p>微信支付</p></div><div><img data-src="/blog/images/%E6%94%AF%E4%BB%98%E5%AE%9D.webp" alt="Frz 支付宝"><p>支付宝</p></div></div></div><div id="copyright"><ul><li class="author"><strong>本文作者： </strong>Frz <i class="ic i-at"><em>@</em></i>物競天擇</li><li class="link"><strong>本文链接：</strong> <a href="https://furanzhang.gitee.io/blog/2021/08/03/%E8%B4%B4%E5%9B%BE2%E6%AC%A1%E5%B9%82%E6%89%B9%E9%87%8F%E4%BF%AE%E6%AD%A3%E5%B7%A5%E5%85%B7%EF%BC%8C%E5%9F%BA%E4%BA%8EPython/" title="贴图2次幂批量修正工具，基于Python">https://furanzhang.gitee.io/blog/2021/08/03/贴图2次幂批量修正工具，基于Python/</a></li><li class="license"><strong>版权声明： </strong>本站所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="ic i-creative-commons"><em>(CC)</em></i>BY-NC-SA</span> 许可协议。转载请注明出处！</li></ul></div></footer></article></div><div class="post-nav"><div class="item left"><a href="/blog/2021/08/03/PS2020%E6%94%AF%E6%8C%81webp%E6%A0%BC%E5%BC%8F/" itemprop="url" rel="prev" data-background-image="&#x2F;blog&#x2F;Shoka_images&#x2F;6833939bly1gicmnywqgpj20zk0m8dwx.jpg" title="PS2020支持webp格式"><span class="type">上一篇</span> <span class="category"><i class="ic i-flag"></i></span><h3>PS2020支持webp格式</h3></a></div><div class="item right"><a href="/blog/2021/08/24/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BAKBEngine%E6%9C%8D%E5%8A%A1%E7%AB%AF/" itemprop="url" rel="next" data-background-image="&#x2F;blog&#x2F;Shoka_images&#x2F;6833939bly1gicmnywqgpj20zk0m8dwx.jpg" title="云服务器搭建KBEngine服务端"><span class="type">下一篇</span> <span class="category"><i class="ic i-flag"></i></span><h3>云服务器搭建KBEngine服务端</h3></a></div></div><div class="wrap" id="comments"></div></div><div id="sidebar"><div class="inner"><div class="panels"><div class="inner"><div class="contents panel pjax" data-title="文章目录"><ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%B8%80-%E5%B7%A5%E5%85%B7%E4%B8%8B%E8%BD%BD"><span class="toc-number">1.</span> <span class="toc-text">一、工具下载</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BA%8C-%E7%94%A8%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">二、用法</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%B8%89-%E5%B7%A5%E5%85%B7%E6%BA%90%E7%A0%81"><span class="toc-number">3.</span> <span class="toc-text">三、工具源码</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%9B%9B-%E5%85%B6%E4%BB%96"><span class="toc-number">4.</span> <span class="toc-text">四、其他</span></a></li></ol></div><div class="related panel pjax" data-title="系列文章"></div><div class="overview panel" data-title="站点概览"><div class="author" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="image" itemprop="image" alt="Frz" data-src="/blog/images/tongZhi.webp"><p class="name" itemprop="name">Frz</p><div class="description" itemprop="description">自卑不是谦逊 | 傲慢不是自信</div></div><nav class="state"><div class="item posts"><a href="/blog/archives/"><span class="count">12</span> <span class="name">文章</span></a></div><div class="item tags"><a href="/blog/tags/"><span class="count">8</span> <span class="name">标签</span></a></div></nav><div class="social"><span class="exturl item github" data-url="aHR0cHM6Ly9naXRodWIuY29tL0ZSWjUyMDEzMTQ=" title="https:&#x2F;&#x2F;github.com&#x2F;FRZ5201314"><i class="ic i-github"></i></span> <span class="exturl item music" data-url="aHR0cHM6Ly9tdXNpYy4xNjMuY29tLyMvdXNlci9ob21lP2lkPTQ2NjY0NTMwNA==" title="https:&#x2F;&#x2F;music.163.com&#x2F;#&#x2F;user&#x2F;home?id&#x3D;466645304"><i class="ic i-cloud-music"></i></span> <span class="exturl item email" data-url="bWFpbHRvOjEwNTA0MTY4ODJAcXEuY29t" title="mailto:1050416882@qq.com"><i class="ic i-envelope"></i></span> <span class="exturl item bilibili" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTIxNzAzMTg2" title="https:&#x2F;&#x2F;space.bilibili.com&#x2F;121703186"><i class="ic i-youtube"></i></span></div><ul class="menu"><li class="item"><a href="/blog/" rel="section"><i class="ic i-home"></i>首页</a></li><li class="item dropdown"><a href="javascript:void(0);"><i class="ic i-feather"></i>文章</a><ul class="submenu"><li class="item"><a href="/blog/archives/" rel="section"><i class="ic i-list-alt"></i>归档</a></li><li class="item"><a href="/blog/categories/" rel="section"><i class="ic i-th"></i>分类</a></li><li class="item"><a href="/blog/tags/" rel="section"><i class="ic i-tags"></i>标签</a></li></ul></li></ul></div></div></div><ul id="quick"><li class="prev pjax"><a href="/blog/2021/08/03/PS2020%E6%94%AF%E6%8C%81webp%E6%A0%BC%E5%BC%8F/" rel="prev" title="上一篇"><i class="ic i-chevron-left"></i></a></li><li class="up"><i class="ic i-arrow-up"></i></li><li class="down"><i class="ic i-arrow-down"></i></li><li class="next pjax"><a href="/blog/2021/08/24/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BAKBEngine%E6%9C%8D%E5%8A%A1%E7%AB%AF/" rel="next" title="下一篇"><i class="ic i-chevron-right"></i></a></li><li class="percent"></li></ul></div></div><div class="dimmer"></div></div></main><footer id="footer"><div class="inner"><div class="widgets"><div class="rpost pjax"><h2>随机文章</h2><ul><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2023/05/04/hexo%E4%BF%AE%E5%A4%8D%E5%9B%BE%E5%BA%8A%EF%BC%8C%E4%BD%BF%E7%94%A8%E9%9D%99%E6%80%81%E5%9B%BE%E7%89%87/" title="hexo修复图床，使用静态图片">hexo修复图床，使用静态图片</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/01/13/hello-world/" title="Hello World">Hello World</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/08/03/PS2020%E6%94%AF%E6%8C%81webp%E6%A0%BC%E5%BC%8F/" title="PS2020支持webp格式">PS2020支持webp格式</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/01/15/hexo%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98%E5%8F%8A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" title="hexo博客搭建中遇到的问题及解决方案">hexo博客搭建中遇到的问题及解决方案</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/08/03/xlua%E7%83%AD%E4%BF%AE%E5%A4%8D%E6%B5%85%E5%B0%9D/" title="xlua热修复浅尝">xlua热修复浅尝</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/08/03/%E8%B4%B4%E5%9B%BE2%E6%AC%A1%E5%B9%82%E6%89%B9%E9%87%8F%E4%BF%AE%E6%AD%A3%E5%B7%A5%E5%85%B7%EF%BC%8C%E5%9F%BA%E4%BA%8EPython/" title="贴图2次幂批量修正工具，基于Python">贴图2次幂批量修正工具，基于Python</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/01/26/Camera-WorldToScreenPoint%E5%A1%AB%E5%9D%91/" title="Camera.WorldToScreenPoint填坑">Camera.WorldToScreenPoint填坑</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/01/16/ps%E4%BF%AE%E6%94%B9%E5%9B%BE%E7%89%87%E6%95%B4%E4%BD%93%E9%A2%9C%E8%89%B2/" title="ps修改图片整体颜色色调">ps修改图片整体颜色色调</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/01/26/unityECS%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/" title="unityECS学习资料">unityECS学习资料</a></span></li><li class="item"><div class="breadcrumb"></div><span><a href="/blog/2021/08/24/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BAKBEngine%E6%9C%8D%E5%8A%A1%E7%AB%AF/" title="云服务器搭建KBEngine服务端">云服务器搭建KBEngine服务端</a></span></li></ul></div><div><h2>最新评论</h2><ul class="leancloud-recent-comment"></ul></div></div><div class="status"><div class="copyright">&copy; 2021 – <span itemprop="copyrightYear">2023</span> <span class="with-love"><i class="ic i-sakura rotate"></i> </span><span class="author" itemprop="copyrightHolder">Frz @ Frz</span></div><div class="count"><span class="post-meta-item-icon"><i class="ic i-chart-area"></i> </span><span title="站点总字数">20k 字</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="ic i-coffee"></i> </span><span title="站点阅读时长">19 分钟</span></div><div class="powered-by">基于 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & Theme.<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2FtZWhpbWUvaGV4by10aGVtZS1zaG9rYQ==">Shoka</span></div></div></div></footer></div><script data-config type="text/javascript">var LOCAL={path:"2021/08/03/贴图2次幂批量修正工具，基于Python/",favicon:{show:"（●´3｀●）やれやれだぜ",hide:"(´Д｀)大変だ！"},search:{placeholder:"文章搜索",empty:"关于 「 ${query} 」，什么也没搜到",stats:"${time} ms 内找到 ${hits} 条结果"},valine:!0,fancybox:!0,copyright:'复制成功，转载请遵守 <i class="ic i-creative-commons"></i>BY-NC-SA 协议。',ignores:[function(e){return e.includes("#")},function(e){return new RegExp(LOCAL.path+"$").test(e)}]}</script><script src="https://cdn.polyfill.io/v2/polyfill.js"></script><script src="//cdn.jsdelivr.net/combine/npm/pace-js@1.0.2/pace.min.js,npm/pjax@0.2.8/pjax.min.js,npm/whatwg-fetch@3.4.0/dist/fetch.umd.min.js,npm/animejs@3.2.0/lib/anime.min.js,npm/algoliasearch@4/dist/algoliasearch-lite.umd.js,npm/instantsearch.js@4/dist/instantsearch.production.min.js,npm/lozad@1/dist/lozad.min.js,npm/quicklink@2/dist/quicklink.umd.js"></script><script src="/blog/js/app.js?v=0.2.5"></script></body></html><!-- rebuild by hrmmi -->